package com.jdk7.mo.writer;

import com.jdk7.mo.entity.Student;
import com.jdk7.mo.repository.StudentRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class StudentItemWriter implements ItemWriter<Student> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    public void setStudentRepository(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }

    public void write(List<? extends Student> students) throws Exception {
        for (Student student : students) {
            studentRepository.save(student);
            logger.info("Update or Insert successful.");
        }
    }
}
